package cn.qayy.dto;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;

import cn.qayy.constants.CollectionConstant;
import cn.qayy.constants.Constant;
import cn.qayy.service.Uploadable;
import cn.qayy.utils.DateUtils;
import cn.qayy.utils.ServletContextUtils;
import cn.qayy.utils.StringUtil;

/**
 * 勘误表DTO
 * 
 * @author jobs1127
 *
 */
@SuppressWarnings("all")
public class ErrataDTO implements Serializable, Uploadable {
	private String zhenghe_qs = "";// QS名(是同一个终端但是名字不同为了统一而取的名字，用于销售口数据统计)
	private String terminaltype = "";// 类型渠道
	private String terminallev = "";// 级别
	private String xieyizhuangtai = "";//条线
	private String fenbu = "";// 分部
	private String daibiao = "";// 代表
	private String tichengbelong = "";// 片区提成人
	private String pmode = "";// 模式：自营、预付、小包、大包、虚拟小包、虚拟大包、承包-小包、承包-预付
	private String money6 = "";//晚交流向标识
	private String status6 = "";//真自然流向标识
	private String province = "";// 省份
	private String city = "";// 地市
	private String county = "";// 区县
	private String daqu = "";// 大区
	private String space = "";// 片区
	private String mgr = "";// 经理
	private String demo = "";// 流向备注
	private String id = "";//ID列(请勿动)
	/**
	 * 设置所有属性
	 */
	private void setAll(String zhenghe_qs,String terminaltype,
			String terminallev,String xieyizhuangtai,String fenbu,
			String daibiao,String tichengbelong,String pmode,
			String money6,String status6,String province,
			String city,String county,String daqu,
			String space,String mgr,String demo, String id) {
		this.zhenghe_qs =zhenghe_qs;
		this.terminaltype = terminaltype;
		this.terminallev = terminallev;
		this.xieyizhuangtai = xieyizhuangtai;
		this.fenbu = fenbu;
		this.daibiao = daibiao;
		this.tichengbelong = tichengbelong;
		this.pmode = pmode;
		this.money6 = money6;
		this.status6 = status6;
		this.province = province;
		this.city = city;
		this.county = county;
		this.daqu = daqu;
		this.space = space;
		this.mgr = mgr;
		this.demo = demo;
		this.id = id;
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getXieyizhuangtai() {
		return this.xieyizhuangtai;
	}

	public void setXieyizhuangtai(String xieyizhuangtai) {
		this.xieyizhuangtai = xieyizhuangtai;
	}

	public String getMoney6() {
		return this.money6;
	}

	public void setMoney6(String money6) {
		this.money6 = money6;
	}

	public String getStatus6() {
		return this.status6;
	}

	public void setStatus6(String status6) {
		this.status6 = status6;
	}

	public String getZhenghe_qs() {
		return zhenghe_qs;
	}

	public void setZhenghe_qs(String zhenghe_qs) {
		this.zhenghe_qs = zhenghe_qs;
	}

	public String getTerminaltype() {
		return terminaltype;
	}

	public void setTerminaltype(String terminaltype) {
		this.terminaltype = terminaltype;
	}

	public String getTerminallev() {
		return terminallev;
	}

	public void setTerminallev(String terminallev) {
		this.terminallev = terminallev;
	}

	public String getFenbu() {
		return fenbu;
	}

	public void setFenbu(String fenbu) {
		this.fenbu = fenbu;
	}

	public String getDaibiao() {
		return daibiao;
	}

	public void setDaibiao(String daibiao) {
		this.daibiao = daibiao;
	}

	public String getTichengbelong() {
		return tichengbelong;
	}

	public void setTichengbelong(String tichengbelong) {
		this.tichengbelong = tichengbelong;
	}

	public String getPmode() {
		return pmode;
	}

	public void setPmode(String pmode) {
		this.pmode = pmode;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCounty() {
		return county;
	}

	public void setCounty(String county) {
		this.county = county;
	}

	public String getDaqu() {
		return daqu;
	}

	public void setDaqu(String daqu) {
		this.daqu = daqu;
	}

	public String getSpace() {
		return space;
	}

	public void setSpace(String space) {
		this.space = space;
	}

	public String getMgr() {
		return mgr;
	}

	public void setMgr(String mgr) {
		this.mgr = mgr;
	}

	public String getDemo() {
		return demo;
	}

	public void setDemo(String demo) {
		this.demo = demo;
	}

	@Override
	public Uploadable filter(Map<String, List<Uploadable>> map, Set<Uploadable> set, int num) {
		/** 容纳错误消息的list */
		List<Uploadable> mlist = new ArrayList<Uploadable>(Constant.SMALL_CAPACITY);
		/** 容纳正确的，最终用于保存到数据库的数据 **/
		List<Uploadable> list = new ArrayList<Uploadable>(Constant.SMALL_CAPACITY);

		/***
		 * 过滤掉所有的空格
		 */
		String zhenghe_qs =StringUtil.trimAll(this.zhenghe_qs);
		String terminaltype = StringUtil.trimAll(this.terminaltype);
		String terminallev = StringUtil.trimAll(this.terminallev);
		String xieyizhuangtai = StringUtil.trimAll(this.xieyizhuangtai);
		String fenbu = StringUtil.trimAll(this.fenbu);
		String daibiao = StringUtil.trimAll(this.daibiao);
		String tichengbelong = StringUtil.trimAll(this.tichengbelong);
		String mode = StringUtil.trimAll(this.pmode);
		String money6 = StringUtil.trimAll(this.money6);
		String status6 = StringUtil.trimAll(this.status6);
		String province = StringUtil.trimAll(this.province);
		String city = StringUtil.trimAll(this.city);
		String county = StringUtil.trimAll(this.county);
		String daqu = StringUtil.trimAll(this.daqu);
		String space = StringUtil.trimAll(this.space);
		String mgr = StringUtil.trimAll(this.mgr);
		String demo = StringUtil.trimAll(this.demo);
		String id = StringUtil.trimAll(this.getId());

		/**
		 * 判断是否导入的模板是正确的模板
		 */
		if (num == 1) {
			if (!CollectionConstant.errata_mb.get(0).contains(zhenghe_qs)
					|| !CollectionConstant.errata_mb.get(1).contains(terminaltype)
					|| !CollectionConstant.errata_mb.get(2).contains(terminallev)
					|| !CollectionConstant.errata_mb.get(3).contains(xieyizhuangtai)
					|| !CollectionConstant.errata_mb.get(4).contains(fenbu)
					|| !CollectionConstant.errata_mb.get(5).contains(daibiao)
					|| !CollectionConstant.errata_mb.get(6).contains(tichengbelong)
					|| !CollectionConstant.errata_mb.get(7).contains(mode)
					|| !CollectionConstant.errata_mb.get(8).contains(money6)
					|| !CollectionConstant.errata_mb.get(9).contains(status6)
					|| !CollectionConstant.errata_mb.get(10).contains(province)
					|| !CollectionConstant.errata_mb.get(11).contains(city)
					|| !CollectionConstant.errata_mb.get(12).contains(county)
					|| !CollectionConstant.errata_mb.get(13).contains(daqu)
					|| !CollectionConstant.errata_mb.get(14).contains(space)
					|| !CollectionConstant.errata_mb.get(15).contains(mgr)
					|| !CollectionConstant.errata_mb.get(16).contains(demo)
					|| !CollectionConstant.errata_mb.get(17).contains(id)) {
				MsgInfo msg = new MsgInfo(Constant.ERROR_MB, Constant.ERROR_MB, Constant.ERROR_MB,
						Constant.ERROR_NUM + num);
				mlist.add(msg);
				map.put("stop", mlist);
				return null;
			}
		} else {
			/***
			 * 获取项目上下文的共享数据
			 */
			ServletContext servletContext = ServletContextUtils.getServletContext();
			/**省份**/
	    	Set<String> provinces = (HashSet<String>)servletContext.getAttribute("provinces");
	    	/**地市**/
	    	Set<String> citys = (HashSet<String>)servletContext.getAttribute("citys");
	    	/**区县**/
	    	Set<String> countrys = (HashSet<String>)servletContext.getAttribute("countrys");
	    	/**大区**/
	    	Set<String> provinceList = (HashSet<String>)servletContext.getAttribute("provinceList");
	    	/**片区**/
	    	Set<String> spaceList = (HashSet<String>)servletContext.getAttribute("spaceList");
	    	/**级别**/
	    	Set<String> terminallveList = (HashSet<String>)servletContext.getAttribute("terminallveList");
	    	
			//数据合格性校验
	    	/***
			 * 判断渠道
			 */
	    	if(StringUtil.isNotBlank(terminaltype)) {
	    		if(!CollectionConstant.qudaos.contains(terminaltype)) {
	    			MsgInfo msg = new MsgInfo(terminaltype,"渠道类型",CollectionConstant.qudaos.toString(),Constant.ERROR_NUM+num);
	    			mlist.add(msg);
	    		}
	    	}
			/***
			 * 判断条线
			 */
	    	if(StringUtil.isNotBlank(xieyizhuangtai)) {
	    		if (!CollectionConstant.txs.contains(xieyizhuangtai)) {
	    			MsgInfo msg = new MsgInfo(xieyizhuangtai, "条线", CollectionConstant.txs.toString(),
	    					Constant.ERROR_NUM + num);
	    			mlist.add(msg);
	    		}
	    	}
			/***
			 * 判断模式
			 */
	    	if(StringUtil.isNotBlank(mode)) {
	    		if (!CollectionConstant.modes.contains(mode)) {
	    			MsgInfo msg = new MsgInfo(mode, "模式", CollectionConstant.modes.toString(), Constant.ERROR_NUM + num);
	    			mlist.add(msg);
	    		}
	    	}
			/***
			 * 判断晚交流向标识
			 */
	    	if (StringUtil.isNotBlank(money6)) {
    			if (!CollectionConstant.wjlx.contains(money6)) {
    				MsgInfo msg = new MsgInfo(money6, "晚交流向标识", CollectionConstant.wjlx.toString(),
    						Constant.ERROR_NUM + num);
    				mlist.add(msg);
    			}
    		}
			/***
			 * 判断真自然流向标识
			 */
	    	if (StringUtil.isNotBlank(status6)) {
    			if (!CollectionConstant.zrlx.contains(status6)) {
    				MsgInfo msg = new MsgInfo(status6, "真自然流向标识", CollectionConstant.zrlx.toString(),
    						Constant.ERROR_NUM + num);
    				mlist.add(msg);
    			}
    		}
			
			/***
			 * 省份、地市、区县是在基础库里出现过
			 */
	    	if (StringUtil.isNotBlank(province)) {
	    		if(!provinces.contains(province)) {
	    			MsgInfo msg = new MsgInfo(province,"省份",Constant.BASIC_DATA,Constant.ERROR_NUM+num);
	    			mlist.add(msg);
	    		}
	    	}
	    	if (StringUtil.isNotBlank(city)) {
	    		if(!citys.contains(city)) {
	    			MsgInfo msg = new MsgInfo(city,"地市",Constant.BASIC_DATA,Constant.ERROR_NUM+num);
	    			mlist.add(msg);
	    		}
	    	}
	    	if (StringUtil.isNotBlank(county)) {
	    		if(!countrys.contains(county)) {
	    			MsgInfo msg = new MsgInfo(county,"区县",Constant.BASIC_DATA,Constant.ERROR_NUM+num);
	    			mlist.add(msg);
	    		}
	    	}
	    	if (StringUtil.isNotBlank(daqu)) {
	    		if(!provinceList.contains(daqu)) {
	    			MsgInfo msg = new MsgInfo(daqu,"大区",Constant.BASIC_DATA,Constant.ERROR_NUM+num);
	    			mlist.add(msg);
	    		}
	    	}
	    	if (StringUtil.isNotBlank(space)) {
	    		if(!spaceList.contains(space)) {
	    			MsgInfo msg = new MsgInfo(space,"片区",Constant.BASIC_DATA,Constant.ERROR_NUM+num);
	    			mlist.add(msg);
	    		}
	    	}
	    	/***
			 * 判断级别
			 */
	    	if(StringUtil.isNotBlank(terminallev)) {
	    		if(!terminallveList.contains(terminallev)) {
	    			MsgInfo msg = new MsgInfo(terminallev,"级别",Constant.CNAME_DATA,Constant.ERROR_NUM+num);
	    			mlist.add(msg);
	    		}
	    	}
			/***
			 * 返回处理后的结果数据
			 */
			if (!mlist.isEmpty()) {
				map.put("fail" + num, mlist);
			}
			/*** 把验证通过的合格数据返回 **/
			this.setAll(zhenghe_qs,terminaltype,
					terminallev,xieyizhuangtai,fenbu,
					daibiao,tichengbelong,mode,
					money6,status6,province,
					city,county,daqu,
					space,mgr,demo,id);
			set.add(this);
			return this;
		}
		return null;
	}

	@Override
	public String flag() {
		return "E";
	}
}
